/*
 * Copyright (C) 2009 Georgios J. Georgopoulos, All rights reserved.
 * 
 * This is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option) any
 * later version.
 * 
 * This software is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
 * site: http://www.fsf.org.
 */
package org.gwt.mosaic.xul.client.ui;

import org.gwt.mosaic.ui.client.HTMLLabel;

import com.google.gwt.user.client.ui.Widget;

/**
 * This element is used to provide a label for a control element. If the user
 * clicks on the label, it will move the focus to the associated control,
 * specified with the control attribute.
 * 
 * @author georgopoulos.georgios(at)gmail.com
 * 
 */
public class Label extends Element {
  private static final long serialVersionUID = -3059444703672407410L;

  private transient HTMLLabel label = null;

  // -----------------------------------------------------------------------
  /**
   * This attribute specifies the id of the element with which the label is
   * associated. When the user clicks on the label, the associated element is
   * given focus.
   */
  public static final String CONTROL = "control";

  public String getControl() {
    return getString(CONTROL);
  }

  public void setControl(String control) {
    putString(CONTROL, control);
  }

  // -----------------------------------------------------------------------
  /**
   * The text to be used for the label.
   */
  public static final String VALUE = "value";

  public String getValue() {
    return getString(VALUE);
  }

  public void setValue(String value) {
    putString(VALUE, value);
  }

  // -----------------------------------------------------------------------
  public Label() {
    super();
  }

  // -----------------------------------------------------------------------
  @Override
  protected Widget createUI() {
    if (label == null) {
      label = new HTMLLabel();
      label.setWordWrap(false);
    }

    syncUI(label);

    return label;
  }

  // -----------------------------------------------------------------------
  @Override
  protected void syncUI(Widget ui) {
    super.syncUI(ui);

    if (attributeMap.containsKey(CONTROL)) {
      label.setLabelFor(getControl());
    }

    if (attributeMap.containsKey(VALUE)) {
      label.setHTML(getValue());
    }
  }

}
